package com.app.business.web.business;

import com.app.business.model.AzProject;
import com.app.business.service.AzProjectService;
import com.app.business.service.CarLotService;
import com.app.business.service.HouseService;
import com.app.business.service.StorageRoomService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 安置项目管理
 * Created by ddz on 2019/2/11.
 */

@Controller
@RequestMapping("/business/azProject/*")
@SuppressWarnings("all")
public class AzProjectController {

    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private DataDictService dataDictService;

    /**
     * 安置项目管理列表
     */
    @RequestMapping("list")
    @RequiresPermissions("azProject:list")
    public void list() {
    }


    /**
     * 加载项目信息数据
     *
     * @return
     */
    @RequestMapping("loadAzProjectList")
    @ResponseBody
    @RequiresPermissions("azProject:loadAzProjectList")
    public Map loadAzProjectList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<AzProject> azProjectPageBean = this.azProjectService.getAzProjectPageBean();
        Map<String, String> townMap = dataDictService.getMapByParentNameForValue_Name("乡镇");
        List<AzProject> azProjectList = azProjectPageBean.getDatas();
        if (!CollectionUtils.isEmpty(azProjectList)) {
            Map<String,Map> houseCountMap = this.houseService.getHouseCountMap();
            Map<String,Map> storageRoomCountMap = this.storageRoomService.getStorageRoomCountMap();
            Map<String,Map> carLotCountMap = this.carLotService.getCarLotCountMap();
            Map<String, Object> item = null;
            for (AzProject azProject : azProjectList) {
                item = null;
                item = Maps.newHashMap();
                Map houseMap = CollectionUtils.isEmpty(houseCountMap.get(azProject.getId().toString())) ? Maps.newHashMap() : houseCountMap.get(azProject.getId().toString());
                Map storageRoomMap = CollectionUtils.isEmpty(storageRoomCountMap.get(azProject.getId().toString())) ? Maps.newHashMap() : storageRoomCountMap.get(azProject.getId().toString());
                Map carLotMap = CollectionUtils.isEmpty(carLotCountMap.get(azProject.getId().toString())) ? Maps.newHashMap() : carLotCountMap.get(azProject.getId().toString());
                String houseTotal = houseMap.get("houseTotal") == null ? "0" : houseMap.get("houseTotal").toString();
                String signHouse = houseMap.get("signHouse") == null ? "0" : houseMap.get("signHouse").toString();
                String storageRoomTotal = storageRoomMap.get("storageRoomTotal") == null ? "0" : storageRoomMap.get("storageRoomTotal").toString();
                String signStorageRoom = storageRoomMap.get("signStorageRoom") == null ? "0" : storageRoomMap.get("signStorageRoom").toString();
                String carLotTotal = carLotMap.get("carLotTotal") == null ? "0" : carLotMap.get("carLotTotal").toString();
                String signCarLot = carLotMap.get("signCarLot") == null ? "0" : carLotMap.get("signCarLot").toString();
                item.put("id", azProject.getId());
                item.put("projectCode", azProject.getProjectCode());
                item.put("projectName", azProject.getProjectName());
                item.put("num", azProject.getNum());
                item.put("town", StringUtils.getNotNull(townMap.get(azProject.getTown())));
                item.put("houseCount", houseTotal+"【<span class='jmdf-color-red'>"+ signHouse +"</span>】");
                item.put("storageRoomCount", storageRoomTotal+"【<span class='jmdf-color-red'>"+ signStorageRoom +"</span>】");
                item.put("carLotCount", carLotTotal+"【<span class='jmdf-color-red'>"+ signCarLot +"</span>】");

                listData.add(item);
            }
        }
        return LayUiUtils.page(azProjectPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 安置项目数据登记
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("form")
    @RequiresPermissions("azProject:form")
    public void form(HttpServletRequest request, ModelMap modelMap) {
        int azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        if (azProject == null) {
            azProject = new AzProject();
        }
        modelMap.addAttribute("azProject", azProject);
    }

    /**
     * 验证安置项目编号是否存在
     *
     * @param request
     */
    @RequestMapping("validateAzProjectCode")
    @ResponseBody
    public synchronized String validateAzProjectCode(HttpServletRequest request) {
        String azProjectCode = ParamUtils.getString(request, "azProjectCode", "");
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        List<AzProject> azProjectList = this.azProjectService.getAzProjectListByAzProjectCode(azProjectCode, azProjectId);
        String msg = "";//返回的验证信息

        if (CollectionUtils.isNotEmpty(azProjectList)) {
            msg = "项目编号已存在";
        }
        return msg;
    }

    /**
     * 安置项目数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("azProjectSave")
    @ResponseBody
    @RequiresPermissions("azProject:azProjectSave")
    public synchronized Response save(HttpServletRequest request) throws AjaxException {
        int azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        if (azProject == null) {
            azProject = new AzProject();
        }
        ServletRequestDataBinder binder = new ServletRequestDataBinder(azProject);
        binder.bind(request);
        this.azProjectService.save(azProject);
        return new Response("0001", "保存成功");
    }

    /**
     * 删除项目数据
     *
     * @param request
     * @return
     */
    @RequestMapping("delAzProjectById")
    @ResponseBody
    @RequiresPermissions("azProject:delAzProjectById")
    public synchronized Response delAzProjectById(HttpServletRequest request) {
        int azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkAjaxArguement(azProject != null, "1111", "数据错误！");
        this.azProjectService.delete(azProject.getId());
        return new Response("0001", "删除成功");
    }

}
